package com.formina.stars;


import java.util.Random;

import javax.microedition.khronos.opengles.GL10;


import com.formina.engine.Engine;
import com.formina.engine.Model;



public class Star extends Model {

		public static int GROWING = 0;
		public static int IDLE = 1;
		public static int ANIMATING = 2;
		
		private int rotationAxis = 0;
		private int mode = 0;
		private float[] growingScale;
		
		private long[] animations = {0,100,200,300,400,500};
		private long animationStart = 0;
		Random rand = new Random();
	 
       public Star(float[] location, float[] scale){
    	   super(location, scale, Engine.Star);
    	   growingScale = new float[]{.01f,.01f,.01f};
    	   rotationAxis = 2;//r.nextInt(3);
    	   this.setRandomColor();
       }
	   
	   public Star() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   @Override
	   public void update(long time){
		   
		   float growrate = .10f;
		 
		   if(this.addedTime==0){
			   addedTime = time;
			   mode = 0;  //growing
		   }
		   switch(mode){
		   		case 0:
		   			if(this.Scale()[0]>this.growingScale[0]&&this.Scale()[1]>this.growingScale[1]&&this.Scale()[2]>this.growingScale[2]){
		 			   growrate = .10f;
		 			   this.growingScale[0]+=growrate;
		 			   this.growingScale[1]+=growrate;
		 			   this.growingScale[2]+=growrate;
		 		   }else{
		 			   mode = 1; //now we're done growing.  idle
		 			   this.growingScale = this.Scale().clone();
		 		   }
		 		   break;
		   		case 1:
		   			if(rand.nextInt(600)<1){
		   				mode = 2;
		   				break;
		   			}
		   			break;
		   		case 2:
		   			growrate=.05f;
		   			if(animationStart ==0){
		   				lastTime = 0;
		   				animationStart = time;
		   				break;
		   			}
		   			
		   			long animTime = lastTime-animationStart;
		   			
		   			if(animTime>=this.animations[0] && animTime <=this.animations[1]){
		   				this.growingScale = this.Scale().clone();
		   			}else if(animTime>=this.animations[1] && animTime <=this.animations[2]){
		   				this.growingScale[0]+=growrate;
			 			this.growingScale[1]+=growrate;
			 			this.growingScale[2]+=growrate;
		   			}else if(animTime>=this.animations[2] && animTime <=this.animations[3]){
		   				this.growingScale[0]-=growrate;
			 			this.growingScale[1]-=growrate;
			 			this.growingScale[2]-=growrate;
		   			}else if(animTime>=this.animations[3] && animTime <=this.animations[4]){
		   				this.growingScale[0]+=growrate;
			 			this.growingScale[1]+=growrate;
			 			this.growingScale[2]+=growrate;
		   			}else if(animTime>=this.animations[4] && animTime <=this.animations[5]){
		   				this.growingScale[0]-=growrate;
			 			this.growingScale[1]-=growrate;
			 			this.growingScale[2]-=growrate;
		   			}else{
		   				//animation is over
		   				animationStart = 0;
		   				mode = 1;
		   			}	
		   			break;
		   }
		   this.Rotation()[rotationAxis]+=.5f;
 		   if(this.Rotation()[rotationAxis]>360){
 			   this.Rotation()[rotationAxis]=1;
 		   }
		   lastTime = time;
		   
	   }
	   
	   @Override
	    public void draw(GL10 gl) {
	    	gl.glPushMatrix();
	    	// Counter-clockwise winding.
			gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
			// Enable face culling.
			gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
			// What faces to remove with the face culling.
			gl.glCullFace(GL10.GL_BACK); // OpenGL docs
				
			gl.glTranslatef(getX(), getY(),getZ());
			gl.glRotatef(Rotation()[0],1f,0,0);
			gl.glRotatef(Rotation()[1],0,1f,0);
			gl.glRotatef(Rotation()[2],0,0,1f);
			gl.glScalef(growingScale[0], growingScale[1], growingScale[2]);
			float[] colorToUse = Color();
			//gl.glColor4f(this.Color[0],this.Color[1],this.Color[2],this.Color[3]);
			if(!Engine.UseRandomColors){
				colorToUse = Engine.DefaultColor;
			}
			gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, colorToUse,0);
			gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE, this.Color(),0);
			gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, new float[]{1f,1f,1f,1f},0);
			gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, 255f/2f);

			// Enabled the vertices buffer for writing and to be used during
			// rendering.
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
			// Specifies the location and data format of an array of vertex
			// coordinates to use when rendering.
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
					modelInfo.vertexBuffer);
			if(modelInfo.Normals.length>0){
				gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
				gl.glNormalPointer(GL10.GL_FLOAT, 0, modelInfo.normalBuffer);

			}
			
			gl.glDrawElements(GL10.GL_TRIANGLES, modelInfo.Indices.length,// OpenGL docs
	    		  GL10.GL_UNSIGNED_SHORT, modelInfo.indexBuffer);
	      		      
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	      
			gl.glDisable(GL10.GL_CULL_FACE);
			if(modelInfo.Normals.length>0){
				gl.glDisable(GL10.GL_NORMALIZE);
				gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
			}
	    	gl.glPopMatrix();

	   }
	   
	}

